<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');


/**
 * This class is for online shop order process - payment.
 * params order step
 * This is the children of controller.
 * PHP5 CMS Typolight
 * Copyright is 34n.sam@gmail.com
 */
 
 /**
 * Start page
 */
session_start();

$orderStatus = new OrderStatus();
$orderStatus->run();

class OrderStatus extends Controller
{	
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();	
		header('Cache-Control: no-store, no-cache, must-revalidate');
		require_once('system/libraries/currency.php');
		$this->obj_currency = new Currency();
	}
	
	/** 
	 * Start page function
	 * @params null
	 * @Return null
	 */
	public function run()
	{
		if (FE_USER_LOGGED_IN)
		{		
			$this->import('FrontendUser', 'User');
			$this->loadDataContainer('tl_member');	
			
			if (!$this->User->id)
				return;
			
			$this->import('Database');
			
			// Path
			$path = "<div class=\"path\"><a title=\"HOME\" href=\"index.php\">HOME</a>&nbsp;" . htmlentities('>') . "&nbsp;<a title=\"MY ACCOUNT\" href=\"profile.html\">" . (($this->User->firstname) ? $this->User->firstname : $this->User->email) . "'s account</a></div><div class=\"quick_tool\">
					<a title=\"ORDER STATUS\" href=\"order-status.html\" class=\"active\">ORDER STATUS</a> <a href=\"profile_detail.html\" title=\"PROFILE\">PROFILE</a> <a title=\"WISHLIST\" href=\"backpocket.html\">WISHLIST</a> <a title=\"ADDRESS BOOK\" href=\"shipping_payment.html\">ADDRESS BOOK</a>
				</div>";
		
			$strOrderStatus = $this->displayOrderStatus($this->User->id);
			
			echo $path . $strOrderStatus;
		}
		else
		{
			$strRedirect = 'sign-in.html';
			$strRedirect .= '?referer=' . base64_encode($this->Environment->request);
			$this->redirect($strRedirect);
		}
	}
	
	/**
	 * Get Order status html
	 * @params string - id_member
	 * @Return string
	 */
	private function displayOrderStatus($id_member=0)
	{
		if (!$id_member)
			return;
			
			
		$this->loadLanguageFile('countries');
		include(TL_ROOT . '/system/config/countries.php');
		
		// Get the states.
		$arrStates = array();
		$objStates = $this->Database->prepare("SELECT * FROM `china_province` ORDER BY `province_en`")
									->execute(time());
		
		while ($objStates->next())
		{
			$arrStates[$objStates->id] = stripslashes($objStates->province_en);
		}
		
		// Get State.
		$arrCitys = array();			
		$objCitys = $this->Database->prepare("SELECT * FROM `china_city` ORDER BY `city_en`")
									->execute(time());
									
		while ($objCitys->next())
		{
			$arrCitys[$objCitys->id] = stripslashes($objCitys->city_en);
		}			
		
		$strHtml = "";
		$strHtml .= "<div id=\"order_status\">";
		
		$objOrders = $this->Database->prepare("SELECT odr.id, odr.orderno, cur.iso_code, odr.total_discounts, IF(odr.dly_firstname<>'', odr.dly_firstname, addr.firstname) AS firstname, IF(odr.dly_lastname<>'', odr.dly_lastname, addr.lastname) AS lastname, IF(odr.dly_street<>'', odr.dly_street, addr.street) AS street, IF(odr.dly_street_2<>'', odr.dly_street_2, addr.street_2) AS street_2, IF(odr.dly_city<>'', odr.dly_city, addr.city) AS city, IF(odr.dly_state<>'', odr.dly_state, addr.state) AS state, IF(odr.dly_country<>'', odr.dly_country, addr.country) AS country, IF(odr.dly_postcode<>'', odr.dly_postcode, addr.postcode) AS postcode, odr_sta.name AS statusname, odr.odr_status, odr.total_paid_real, odr.total_shipping, odr.total_catolog, odr.date_add, odr.date_ship, odr.total_paid, odr.payment, odr.`shipping_id` AS ship_id   
												FROM tl_orders AS odr
												LEFT JOIN tl_address_book AS addr ON odr.id_address_delivery = addr.id
												LEFT JOIN tl_order_history AS odr_h ON odr_h.id_order=odr.id
												LEFT JOIN tl_order_state_lang AS odr_sta ON odr_h.id_order_state=odr_sta.id_order_state AND odr_sta.id_lang=odr.id_lang
												LEFT JOIN tl_currency AS cur ON cur.id=odr.id_currency 
												WHERE id_member=?
												Group by odr.orderno
												ORDER BY date_upd DESC")
									->execute($id_member);
		
		while ($objOrders->next())
		{
			$arr_shippings = array();
			if ($objOrders->ship_id)
			{
				$obj_select_shipping = $this->Database->prepare("SELECT * FROM `tl_shippings` WHERE `id`=?")
														->execute($objOrders->ship_id);
				$arr_shippings = array(
					'name'=>$obj_select_shipping->name,
					'ship_des'=>$obj_select_shipping->ship_des,
				);
			}
			
			if($objOrders->street_2=="line 2" || $objOrders->street_2=="Address line 2") {
				$street = $objOrders->street . ", ";
			}else{
				$street = $objOrders->street . "<br />" .$objOrders->street_2. ", ";
			}			
			
			$strHtml .= "<form id=\"form_odr_payment_" . $objOrders->id . "\" action=\"orderPayment.html\" name=\"order_payment\" method=\"post\"><div class=\"order_placed_item\">
				<h1><u><a href=\"order_sumary.html?id_order=" . $objOrders->id . "\" title=\"order sumary\">ORDER #" . $objOrders->orderno . "</a></u> <small>(Placed " . date('m/d/Y', strtotime($objOrders->date_add)) . ")</small></h1>
				<table class=\"order_placed\">
				  <tr>
					<td rowspan=\"2\" class=\"order_address\">
						<h3>SHIPPING ADDRESS</h3>
						<p>" . $objOrders->firstname . " " . $objOrders->lastname . ",  <br />" . $street . (($arrCitys[$objOrders->city]&&$objOrders->country=='cn') ? $arrCitys[$objOrders->city] : $objOrders->city) . "<br />" . (($arrStates[$objOrders->state]&&$objOrders->country=='cn') ? $arrStates[$objOrders->state] : $objOrders->state) . "&nbsp;" . $objOrders->postcode . "&nbsp;" . ($countries[$objOrders->country] ? $countries[$objOrders->country] : $objOrders->country) . "</p>
					</td>
					<td rowspan=\"2\" class=\"order_method\">
						<h3>SHIPPING METHOD</h3>
						" . (($arr_shippings) ? "<p>".$arr_shippings['name']."</p>" . $arr_shippings['ship_des'] : (($objOders->shipping_fee == "10.00") ? "<p>STANDARD DELIVERY: ￥10.00</p><p><small>Your package will arrive within 2~3 days.</small></p>" : (($objOders->shipping_fee == "0.00") ? "￥ 0.00 - free shopping till August 31" : "<p>SAME DAY DELIVERY (SHANGHAI ONLY): ￥25.00</p><p><small>Your package will arrive within 24 hours after order is placed</small></p>"))) . "
					</td>
					<td rowspan=\"2\" class=\"order_status\">
						<h3>SHIPPING STATUS</h3>
						<p>" . (($objOrders->statusname == 'Payment Pending' && $objOrders->payment == 'COD') ? 'Preparation in Progress' : $objOrders->statusname) . (($objOrders->odr_status < '3' || $objOrders->odr_status == '6') ? "" : "</p>
						<!--<small>Your order was shipped on " . date('m/d/Y', strtotime($objOrders->date_ship)) . ". <br/>For more details regarding your order <br/>please click on the order number shown above.</small>--></p>") . "
					</td>
					<td class=\"order_price\">
						<p><span class=\"label\">SUBTOTAL:</span> <span class=\"price\">" . $this->obj_currency->change_order($objOrders->total_catolog,$objOrders->iso_code) . "</span></p>
						<p><span class=\"label\">SHIPPING:</span> <span class=\"price\">" . $this->obj_currency->change_order($objOrders->total_shipping,$objOrders->iso_code) . "</span></p>
						" . (($objOrders->total_discounts > 0) ? "<p><span class=\"label\">GIFT VOUCHER:</span> <span class=\"price\">" . $this->obj_currency->change_order($objOrders->total_discounts,$objOrders->iso_code) . "</span></p>" : "") . "
					</td>
				  </tr>
				  <tr>
					<td class=\"order_price order_totalprice\"><p><span class=\"label\">TOTAL:</span> <span class=\"price\">" . $this->obj_currency->change_order($objOrders->total_paid_real,$objOrders->iso_code) . "</span></p>" . (($objOrders->payment != 'COD' && $objOrders->odr_status < 2) ? "
						<p><a href=\"javascript:void(0);\" style=\"color:Red;font-style:italic\" order_id=\"" . $objOrders->id . "\" class=\"re_payment\">Problem with making payment?<br />Try again</a></p>" : "") . "</td>
				  </tr>
				</table> <!-- order_placed end -->
			</div>
			<input type=\"hidden\" name=\"id_order\" value=\"" . $objOrders->id . "\" />
			</form>
			<!-- order_placed_item end -->";
		}
		
		$strHtml .= "</div><script type=\"text/javascript\">
				jQuery(function(){
					jQuery(\".re_payment\").click(function(e){
						var _target = $(e.target) || $(e);
						var _form = jQuery(\"#form_odr_payment_\"+_target.attr('order_id'));
						if (_target.attr('order_id')&&_form.html()){
							_form.submit();
						}
					});
				});
			</script>";
		
		return $strHtml;
	}
}